package com.util;

import com.bean.PayBean;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cxt
 * @time: 2021/3/29
 * 树形数据工具
 * 注意，传入的对象属性中父级id名为pid，如果不是就将下面 “pid” 名改成自己的
 */
@Data
@Slf4j
public class TreeModelUtil<T> {

    public static List<Object> buildTreeList(List<?> tm) {
        if (tm.size() == 0) {
            return null;
        }
        List<Object> list = new ArrayList<>(); // 最后返回的数据
        try {
            for (Object obj : tm) {
                Map<String, Object> mapArr = new LinkedHashMap<>();
                // 取值
                Class<?> clz = obj.getClass();
                Field[] temp = clz.getDeclaredFields();
                boolean msg = false;
                for (Field f : temp) {
                    f.setAccessible(true); // 可以访问私有变量
                    String value = String.valueOf(f.get(obj));
                    if (f.getName().equals("pid") && !value.equals("")) {
                        msg = true;
                        break;
                    }
                    mapArr.put(f.getName(), value);
                }
                if (msg) {
                    continue;
                }
                // 查找child
                String id = (String) mapArr.get("id");
                mapArr.put("child", getTreeList(id, tm));
                list.add(mapArr);
            }
        } catch (IllegalAccessException e) {
            log.error("树形工具类出错");
        }
        return list;
    }

    private static List<?> getTreeList(String id, List<?> treeModels) {
        List<Object> lists = new ArrayList<>();
        try {
            for (Object obj : treeModels) {
                Map<String, Object> mapArr = new LinkedHashMap<>();
                // 取值
                Class<?> clz = obj.getClass();
                Field[] temp = clz.getDeclaredFields();
                boolean msg = false;
                for (Field f : temp) {
                    f.setAccessible(true); // 可以访问私有变量
                    String value = String.valueOf(f.get(obj));
                    if (f.getName().equals("pid") && !value.equals(id)) {
                        msg = true;
                        break;
                    }
                    mapArr.put(f.getName(), value);
                }
                if (msg) {
                    continue;
                }
                // 查找child
                String childId = (String) mapArr.get("id");
                mapArr.put("child", getTreeList(childId, treeModels));
                lists.add(mapArr);
            }
        } catch (IllegalAccessException e) {
            log.error("树形工具类出错");
        }
        return lists;
    }

    public static void main(String[] args) {
        ArrayList<PayBean> list = new ArrayList<>();
        PayBean bean0 = new PayBean(1, "第一级", 0);
        PayBean bean1 = new PayBean(2, "第一级", 1);
        PayBean bean2 = new PayBean(3, "第一级", 2);
        PayBean bean3 = new PayBean(4, "第一级", 4);
        list.add(bean0);
        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        List<Object> objects = buildTreeList(list);

        System.out.println(objects);
    }


    /**
     * 获取当前字典的子字典集合
     */
//    private List<T> getChildrenMenus(List<T> menuList, Long parentId, String pCode) {
//        return menuList.stream().filter(child -> {
//            Class<?> clz = child.getClass();
//            Field[] temp = clz.getDeclaredFields();
//            for (Field f : temp) {
//                f.setAccessible(true); // 可以访问私有变量
//                String value = null;
//                try {
//                    value = String.valueOf(f.get(child));
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//
//                if (f.getName().equals("pid") && !value.equals(id)) {
//
//                    break;
//                }
//            }
//
//            Objects.equals(child.getPid(), parentId);
//            return true;
//        }).collect(Collectors.toList());
//    }
//
//    /**
//     * 获取树形字典
//     */
//    private List<T> getTreeMenus(List<T> allMenus, List<T> curMenus) {
//        return curMenus.stream().peek(mission -> mission.setChildren(getTreeMenus(allMenus, getChildrenMenus(allMenus, mission.getId())))).collect(Collectors.toList());
//    }

}
